package com.cykj.faceledtv.data.db;


import com.cykj.faceledtv.data.db.model.DaoMaster;
import com.cykj.faceledtv.data.db.model.DaoSession;
import com.cykj.faceledtv.data.db.model.UserData;

import javax.inject.Inject;
import javax.inject.Singleton;


/**
 * 数据库操作业务具体实现类
 *
 * @author jzh
 * @date 2018/12/11 17:24
 */
@Singleton
public class AppDbHelper implements DbHelper {

    private final DaoSession mDaoSession;

    @Inject
    public AppDbHelper(DbOpenHelper dbOpenHelper) {
        mDaoSession = new DaoMaster(dbOpenHelper.getWritableDb()).newSession();
    }


    @Override
    public boolean insertOrReplaceUserData(UserData userData) {
        if (userData == null) {
            return false;
        }
        try {
            mDaoSession.getUserDataDao().insertOrReplace(userData);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public boolean deleteUserDataById(Long id) {
        if (id == null) {
            return false;
        }
        try {
            mDaoSession.getUserDataDao().deleteByKey(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteAllUserData() {
        try {
            mDaoSession.getUserDataDao().deleteAll();
            return true;
        } catch (Exception E) {
            return false;
        }
    }

    @Override
    public UserData getUserDataById(Long id) {
        try {
            return mDaoSession.getUserDataDao().load(id);
        } catch (Exception e) {
            return null;
        }
    }
}
